import { HttpError, toSuccess, useEntityManager } from "@sker/core";
import { AutoUser } from "@sker/entities";
import { BaiYunCore } from "./core";

const appid = `wxaadbab9d13edff20`

export default async () => {
    return await useEntityManager(async m => {
        const user = await m.findOne(AutoUser, { where: { mobile: `13837526895` } })
        if (!user) throw new HttpError(`参数错误`)
        const setting = user.setting;
        if (!setting) throw new HttpError(`参数错误`)
        const baiyunSetting = setting.baiyun;
        const baiyun = new BaiYunCore(baiyunSetting.account, baiyunSetting.password)
        try {
            const res = await baiyun.auto(appid)
            if (res) {
                return toSuccess(res)
            } else {
                throw new HttpError(`操作失败`)
            }
        } catch (e) {
            throw new HttpError(e.message)
        }
    })
}
